package com.woshua.qmui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;
import com.woshua.qmui.R;

import static com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView.SEPARATOR_STYLE_NONE;

/**
 * @author Steven.zhao
 * email:hongtu.zhao@goodwinsoft.net
 * date:2019/6/19
 * desc:
 */
public class NewsClassfiFragment extends Fragment {

    private QMUIGroupListView groupListView;

    public static NewsClassfiFragment newInstance(int type) {
        Bundle bundle = new Bundle();
        NewsClassfiFragment fragment = new NewsClassfiFragment();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        groupListView = view.findViewById(R.id.groupListView);


        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v instanceof QMUICommonListItemView) {
                    CharSequence text = ((QMUICommonListItemView) v).getText();
                    Toast.makeText(getActivity(), text + " is Clicked", Toast.LENGTH_SHORT).show();
                }

                new QMUIBottomSheet.BottomListSheetBuilder(getContext())
                        .addItem("Item 1")
                        .addItem("Item 2")
                        .addItem("Item 3")
                        .setOnSheetItemClickListener((dialog, itemView, position, tag) -> {
                            dialog.dismiss();
                            Toast.makeText(getActivity(), "Item " + (position + 1), Toast.LENGTH_SHORT).show();
                        })
                        .build()
                        .show();
            }
        };

        QMUIGroupListView.Section section = QMUIGroupListView.newSection(getContext());
        section.setSeparatorDrawableRes(1, R.color.qmui_config_color_white, R.drawable.divider_separator, R.drawable.divider_separator);
        for (int i = 0; i < 3; i++) {
            section.addItemView(createListView(), onClickListener);
        }
        section.addTo(groupListView);

        QMUIGroupListView.Section section2 = QMUIGroupListView.newSection(getContext());
        section2.setSeparatorDrawableRes(1, R.color.qmui_config_color_white, R.drawable.divider_separator, R.drawable.divider_separator);
        for (int i = 0; i < 3; i++) {
            section2.addItemView(createListView(), onClickListener);
        }
        section2.addTo(groupListView);

        QMUIGroupListView.Section section3 = QMUIGroupListView.newSection(getContext());
        section3.setSeparatorDrawableRes(1, R.color.qmui_config_color_white, R.drawable.divider_separator, R.drawable.divider_separator);
        for (int i = 0; i < 3; i++) {
            section3.addItemView(createListView(), onClickListener);
        }
        section3.addTo(groupListView);
    }

    private QMUICommonListItemView createListView() {
        QMUICommonListItemView itemMyWallet = groupListView.createItemView(
                ContextCompat.getDrawable(getContext(), R.mipmap.icon_wallet),
                "我的钱包",
                null,
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_CUSTOM,
                QMUIDisplayHelper.dp2px(getContext(), 50));

        itemMyWallet.getDetailTextView().setVisibility(View.GONE);

        ImageView imageView = new ImageView(getContext());
        imageView.setImageResource(R.mipmap.button_forward);
        itemMyWallet.addAccessoryCustomView(imageView);

        return itemMyWallet;
    }
}
